﻿using System.ComponentModel;
using System.Reflection;

namespace Foundation.Core
{
    public class ConstantSelectCreateExecutor : ITypeSelectCreateExecutor, IConstantSelectFilterMetadata
    {
        public bool CanHandle(Type type)
        {
            return !type.IsAbstract;
        }

        public object CreateModel(Type type) => null;

        public IList<MemberInfo> GetMemberInfos(Type type)
        {
            var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);

            return fields.ToArray();
        }

        public void Execute(TypeSelectCreateExecuteContext context)
        {
            if (context.MemberInfo is FieldInfo info)
            {
                context.Name = info.Name;
                context.Label = info.GetCustomAttribute<DescriptionAttribute>()?.Description ?? info.Name;
            }
        }
    }
}
